package msr.frontend;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import msr.object.DPLN;
import msr.object.Yoyaku;
import msr.service.OracleService;
import msr.util.DateParse;
import msr.util.RequestAction;

import com.opensymphony.xwork2.ActionSupport;

public class SearchDplnController extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<DPLN> rtList;
	private String sup_year;
	private String sup_month;
	private String productcode;
	private String feedCD;
	private List<Yoyaku> rtListYoyaku;
	OracleService oService = new OracleService();

	public String execute() {
		String month = this.editMonth(sup_month);
		String supdate = null;
		if (month != null && !month.equals("0"))
			supdate = sup_year + month;
		else
			supdate = sup_year;
		System.out.println("supdate " + supdate);
		System.out.println("code " + productcode);
		Map<String, Object> params = new HashMap<String, Object>();

		DateParse dParse = new DateParse();
		int date = Integer.parseInt(dParse.gettime());
		System.out.println("value of date " + date);
		params.put("date", date);
		params.put("supdate", supdate);
		params.put("feedcd", productcode);
		if (productcode == null && supdate == null) {
			rtList = oService.getListDpln(params, RequestAction.GET_ALL_DPLN);
		} else if (productcode.equals("") && Integer.parseInt(supdate) == 0) {
			rtList = oService.getListDpln(params, RequestAction.GET_ALL_DPLN);
		} else {
			rtList = oService
					.getListDpln(params, RequestAction.GET_CUSTOM_DPLN);
		}
		if (rtList == null) {
			addActionError(getText("error.historynotfound"));
			return ERROR;
		}
		return SUCCESS;
	}

	public String searchYoyakuList() {
		Map<String, String> params = new HashMap<String, String>();
		System.out.println(feedCD);
		if (feedCD == null || feedCD.equals("")) {
			// need edit here
			rtListYoyaku = oService.getListYoyaku(params,
					RequestAction.GET_ALL_YOYAKU);
		} else {
			// need edit here
			params.put("feedCD", feedCD);
			rtListYoyaku = oService.getListYoyaku(params,
					RequestAction.GET_CUSTOM_YOYAKU);
		}
		if (rtListYoyaku == null) {
			addActionError(getText("error.historynotfound"));
			return ERROR;
		}
		return SUCCESS;
	}

	private String editMonth(String month) {
		String rt = null;
		if (month != null)
			if (Integer.parseInt(month) < 10 && Integer.parseInt(month) > 0)
				rt = "0" + month;
			else
				rt = month;
		return rt;
	}

	public List<DPLN> getRtList() {
		return rtList;
	}

	public void setRtList(List<DPLN> rtList) {
		this.rtList = rtList;
	}

	public String getSup_year() {
		return sup_year;
	}

	public void setSup_year(String sup_year) {
		this.sup_year = sup_year;
	}

	public String getSup_month() {
		return sup_month;
	}

	public void setSup_month(String sup_month) {
		this.sup_month = sup_month;
	}

	public String getProductcode() {
		return productcode;
	}

	public void setProductcode(String productcode) {
		this.productcode = productcode;
	}

	public List<Yoyaku> getRtListYoyaku() {
		return rtListYoyaku;
	}

	public void setRtListYoyaku(List<Yoyaku> rtListYoyaku) {
		this.rtListYoyaku = rtListYoyaku;
	}

	public String getFeedCD() {
		return feedCD;
	}

	public void setFeedCD(String feedCD) {
		this.feedCD = feedCD;
	}

}
